﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Data.Model;
using NHibernate;
using Senparc.Weixin.MP.Entities;
using NHibernate.Criterion;
using System.Collections.Generic;
using ZLCMS.Business.Util;
using Senparc.Weixin.MP.Helpers;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Business.UploadFileInfo;
using ZLCMS.Business.WeixinService;

namespace ZLCMS.Web.SystemManage.weixin
{
    public partial class EditShopHome : System.Web.UI.Page
    {
        protected FrameHelper _frameHelper;

        protected string _userAccount;
        protected WxMaterial _mat;
        protected WxMaterialNews _news;
        protected WxConfig _config;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            _frameHelper = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();
            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                Save();
            }
        }

        #region 保存操作(新建/更新) + private void Save()
        /// <summary>
        /// 保存操作
        /// </summary>
        private void Save()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                try
                {
                    if (_mat == null)
                    {
                        _mat = new WxMaterial();
                        _mat.MsgType = "shop";
                        _mat.CreateDate = DateTime.Now;
                        _mat.Status = "1";
                    }

                    _mat.UpdateDate = DateTime.Now;
                    _mat.Title = tb_name.Text;
                    _mat.OwnerAccount = _userAccount;

                    session.SaveOrUpdate(_mat);

                    if (_news == null)
                    {
                        _news = new WxMaterialNews();
                        _news.MaterialId = _mat.Id;
                    }

                    _news.Title = tb_name.Text;
                    _news.Description = tb_desc.Text;
                    _news.PicUrl = fc_open.Value;
                    _news.Url = WeixinUtil.WxShopHtmlUrl(_userAccount, "");
                    _news.ContentType = "URL";
                    _news.Content = txtContent.Value;
                    _news.OwnerAccount = _userAccount;
                    session.SaveOrUpdate(_news);

                    if (_config == null)
                    {
                        _config = new WxConfig();
                        _config.OwnerAccount = _userAccount;
                    }
                    _config.IsShopPurchasable = cbxIsPurchasable.Checked == true ? "1" : "0";
                    
                    session.SaveOrUpdate(_config);
                    session.Flush();
                }
                catch (System.Exception ex)
                {
                    _frameHelper.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }

            _frameHelper.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                navTabId = _frameHelper.NavTabId
            });
        }
        #endregion

        #region 初始化全局变量 + protected void InitializeGlobalValue()
        /// <summary>
        /// 初始化全局变量
        /// </summary>
        protected void InitializeGlobalValue()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                try
                {
                    _mat = session.CreateCriteria<WxMaterial>().Add(Expression.Eq("OwnerAccount", _userAccount)).Add(Expression.Eq("MsgType", "shop")).UniqueResult<WxMaterial>();

                    if (_mat != null)
                    {
                        _news = session.CreateCriteria<WxMaterialNews>().Add(Expression.Eq("OwnerAccount", _userAccount)).Add(Expression.Eq("MaterialId", _mat.Id)).UniqueResult<WxMaterialNews>();
                        _config = session.CreateCriteria<WxConfig>().Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxConfig>();
                    }
                }
                catch (System.Exception ex)
                {
                    _frameHelper.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }
        }
        #endregion

        #region 初始化控件值 + protected void InitializeContent()
        /// <summary>
        /// 初始化控件值
        /// </summary>
        protected void InitializeContent()
        {
            //页面控件值
            if (!IsPostBack)
            {
                if (_mat != null)
                {
                    tb_name.Text = _mat.Title;
                    tb_desc.Text = _news.Description;
                    txtContent.Value = _news.Content;
                    fc_open.Value = _news.PicUrl;
                    if (_config != null)
                    {
                        cbxIsPurchasable.Checked = _config.IsShopPurchasable == "1" ? true : false;
                    }
                }
                else
                {
                    tb_name.Text = "添加商城";
                    tb_desc.Text = "亲，请点击进入添加商城，感谢您的参与!";
                    txtContent.Value = "亲，请点击进入添加商城，感谢您的参与!";
                }

                fc_open.Config = new UploadConfig()
                {
                    UploadDir = _userAccount + "/ShopHome/",
                    Thumbnail = new ThumbnailConfig()
                    {
                        Auto = true,
                        Height = 200,
                        Width = 360,
                        Mode = "HW"
                    }
                };
            }
        }
        #endregion
    }
}